package com.example.springboot.service.impl;

import com.example.springboot.controller.request.BasePageRequest;
import com.example.springboot.entity.Category;
import com.example.springboot.mapper.CategoryMapper;
import com.example.springboot.service.CategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * ClassName:CategoryServiceImpl
 * Package:com.example.springboot.service.impl
 * Description:
 *
 * @Author: Gao
 * @Create: 2023/2/6 - 14:36
 */
@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public List<Category> list() {

        return categoryMapper.list();
    }

    @Override
    public Object page(BasePageRequest basePageRequest) {
        PageHelper.startPage(basePageRequest.getPageNum(), basePageRequest.getPageSize());
        List<Category> categories = categoryMapper.listByCondition(basePageRequest);
        return new PageInfo<>(categories);
    }

    @Override
    public void save(Category category) {
        categoryMapper.save(category);
    }

    @Override
    public void deleteById(int id) {
        categoryMapper.deleteById(id);
    }

    @Override
    public void update(Category category) {
        category.setUpdatetime( new Date());
        categoryMapper.updateById(category);
    }

    @Override
    public Category getById(int id) {
        return categoryMapper.getById(id);
    }

    @Override
    public Category getByName(String name) {
        return categoryMapper.getByName(name);
    }
}
